<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>vue-源码笔记-初始化方法分析 | lafen | lafen&#39;s personal blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="learning,vue">
    <meta name="description" content="前言终于还是开始了菜鸟的 vue 源码之旅，磕磕绊绊、断断续续的看过来也有三四个月时间了，虽然还是有很多看不明白的地方，但是总想先记录下自己的学习过程，这样会使自己更有动力去学习 开始我的所采用的办法很低效，就是打断点，一步一步的看 Vue 在我的例子中做了什么事情，最简单的实例如下，接下来就看看 Vue 是如何实例化、如何初始化到我们的页面上的吧 123456789101112131415161">
<meta name="keywords" content="learning,vue">
<meta property="og:type" content="article">
<meta property="og:title" content="vue-源码笔记-初始化方法分析">
<meta property="og:url" content="https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/index.html">
<meta property="og:site_name" content="lafen">
<meta property="og:description" content="前言终于还是开始了菜鸟的 vue 源码之旅，磕磕绊绊、断断续续的看过来也有三四个月时间了，虽然还是有很多看不明白的地方，但是总想先记录下自己的学习过程，这样会使自己更有动力去学习 开始我的所采用的办法很低效，就是打断点，一步一步的看 Vue 在我的例子中做了什么事情，最简单的实例如下，接下来就看看 Vue 是如何实例化、如何初始化到我们的页面上的吧 123456789101112131415161">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-11-30T09:04:39.251Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue-源码笔记-初始化方法分析">
<meta name="twitter:description" content="前言终于还是开始了菜鸟的 vue 源码之旅，磕磕绊绊、断断续续的看过来也有三四个月时间了，虽然还是有很多看不明白的地方，但是总想先记录下自己的学习过程，这样会使自己更有动力去学习 开始我的所采用的办法很低效，就是打断点，一步一步的看 Vue 在我的例子中做了什么事情，最简单的实例如下，接下来就看看 Vue 是如何实例化、如何初始化到我们的页面上的吧 123456789101112131415161">
    
        <link rel="alternate" type="application/atom+xml" title="lafen" href="/atom.xml">
    
    <link rel="shortcut icon" href="/img/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide">
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">lafen</h5>
          <a href="mailto:1031822863@qq.com" title="1031822863@qq.com" class="mail">1031822863@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/">
                <i class="icon icon-lg icon-home"></i>
                home
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives">
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags">
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories">
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/zhongxiaotian" target="_blank">
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://www.weibo.com/" target="_blank">
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://gitee.com/lafen" target="_blank">
                <i class="icon icon-lg icon-link"></i>
                Gitee
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">vue-源码笔记-初始化方法分析</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">vue-源码笔记-初始化方法分析</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-11-30T09:03:42.000Z" itemprop="datePublished" class="page-time">
  2019-11-30
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/">vue</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/learning/">learning</a></li></ul></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#前言"><span class="post-toc-number">1.</span> <span class="post-toc-text">前言</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#开始"><span class="post-toc-number">2.</span> <span class="post-toc-text">开始</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#初始化阶段"><span class="post-toc-number">3.</span> <span class="post-toc-text">初始化阶段</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#initMixin-Vue"><span class="post-toc-number">3.1.</span> <span class="post-toc-text">initMixin(Vue)</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#stateMixin-Vue"><span class="post-toc-number">3.2.</span> <span class="post-toc-text">stateMixin(Vue)</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#eventsMixin-Vue"><span class="post-toc-number">3.3.</span> <span class="post-toc-text">eventsMixin(Vue)</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#lifecycleMixin-Vue"><span class="post-toc-number">3.4.</span> <span class="post-toc-text">lifecycleMixin(Vue)</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#renderMixin-Vue"><span class="post-toc-number">3.5.</span> <span class="post-toc-text">renderMixin(Vue)</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#initGlobalAPI-Vue"><span class="post-toc-number">3.6.</span> <span class="post-toc-text">initGlobalAPI(Vue)</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#比较重要的方法分析"><span class="post-toc-number">4.</span> <span class="post-toc-text">比较重要的方法分析</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#set-方法分析"><span class="post-toc-number">4.1.</span> <span class="post-toc-text">$set 方法分析</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#缺陷"><span class="post-toc-number">4.1.1.</span> <span class="post-toc-text">缺陷</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#方法"><span class="post-toc-number">4.1.2.</span> <span class="post-toc-text">方法</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#方法实现"><span class="post-toc-number">4.1.3.</span> <span class="post-toc-text">方法实现</span></a></li></ol></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#delete方法分析"><span class="post-toc-number">4.2.</span> <span class="post-toc-text">$delete方法分析</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#缺陷-1"><span class="post-toc-number">4.2.1.</span> <span class="post-toc-text">缺陷</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#方法实现-1"><span class="post-toc-number">4.2.2.</span> <span class="post-toc-text">方法实现</span></a></li></ol></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#on-方法分析"><span class="post-toc-number">4.3.</span> <span class="post-toc-text">$on 方法分析</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#once-方法分析"><span class="post-toc-number">4.4.</span> <span class="post-toc-text">$once 方法分析</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#off-方法分析"><span class="post-toc-number">4.5.</span> <span class="post-toc-text">$off 方法分析</span></a></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析" class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">vue-源码笔记-初始化方法分析</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-11-30 17:03:42" datetime="2019-11-30T09:03:42.000Z" itemprop="datePublished">2019-11-30</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/">vue</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/learning/">learning</a></li></ul></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style="display:none">
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h4 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h4><p>终于还是开始了菜鸟的 <code>vue</code> 源码之旅，磕磕绊绊、断断续续的看过来也有三四个月时间了，虽然还是有很多看不明白的地方，但是总想先记录下自己的学习过程，这样会使自己更有动力去学习</p>
<h4 id="开始"><a href="#开始" class="headerlink" title="开始"></a>开始</h4><p>我的所采用的办法很低效，就是打断点，一步一步的看 <code>Vue</code> 在我的例子中做了什么事情，最简单的实例如下，接下来就看看 <code>Vue</code> 是如何实例化、如何初始化到我们的页面上的吧</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>Vue.js v2.6.10<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>&gt;</span></span><br><span class="line">    &#123;&#123; message &#125;&#125;</span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"vue2.js"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="actionscript">  <span class="keyword">var</span> app = <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="actionscript">    el: <span class="string">'#app'</span>,</span></span><br><span class="line"><span class="undefined">    data: &#123;</span></span><br><span class="line"><span class="actionscript">      message: <span class="string">'Hello Vue!'</span></span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined">  &#125;)</span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="初始化阶段"><a href="#初始化阶段" class="headerlink" title="初始化阶段"></a>初始化阶段</h4><p><code>vue</code> 在实例化之前的<strong>初始化阶段</strong>所执行的函数，先依次看看函数中分别做了什么事情</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">initMixin(Vue);</span><br><span class="line">stateMixin(Vue);</span><br><span class="line">eventMixin(Vue);</span><br><span class="line">lifecycleMixin(Vue);</span><br><span class="line">renderMixin(Vue);</span><br><span class="line"></span><br><span class="line">initGlobalAPI(Vue);</span><br></pre></td></tr></table></figure>
<h5 id="initMixin-Vue"><a href="#initMixin-Vue" class="headerlink" title="initMixin(Vue)"></a>initMixin(Vue)</h5><p><code>initMixin(Vue)</code> 所做的事情只是将 <code>_init</code> 函数挂载到构造函数 <code>Vue</code> 的原型上，先不管执行，只看函数里面声明了什么</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">initMixin</span> (<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">    Vue.prototype._init = <span class="function"><span class="keyword">function</span>(<span class="params">options</span>) </span>&#123;</span><br><span class="line">        <span class="comment">// code...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>_init</code> 函数是 <code>Vue</code> <strong>实例初始化</strong>的函数，其中包括了 <code>beforeCreate</code> 、 <code>created</code> 、 <code>beforeMount</code> 和 <code>mounted</code> <strong>四个生命周期函数</strong>，现在暂且不细看里面的实现如何，到实例化 <code>vue</code>  的时候再细讲，先往下看 <code>stateMixin(Vue)</code></p>
<h5 id="stateMixin-Vue"><a href="#stateMixin-Vue" class="headerlink" title="stateMixin(Vue)"></a>stateMixin(Vue)</h5><p>只是在这个方法中，给 <code>Vue</code> 原型挂载了几个方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">stateMixin</span> (<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 对 $date 和 $props 做一下代理，this.$data访问_data</span></span><br><span class="line">    <span class="built_in">Object</span>.defineProperty(Vue.prototype, <span class="string">'$data'</span>, dataDef);</span><br><span class="line">    <span class="built_in">Object</span>.defineProperty(Vue.prototype, <span class="string">'$props'</span>, propsDef);</span><br><span class="line">    <span class="comment">// 挂载 $set 和 $delete 方法，set 和 del 后面再说</span></span><br><span class="line">    Vue.prototype.$set = set;</span><br><span class="line">    Vue.prototype.$<span class="keyword">delete</span> = del;</span><br><span class="line">    <span class="comment">// 挂载 $watch 方法</span></span><br><span class="line">    Vue.prototype.$watch = <span class="function"><span class="keyword">function</span>(<span class="params">expOrFn, cb, options</span>) </span>&#123;</span><br><span class="line">        <span class="comment">// code...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="eventsMixin-Vue"><a href="#eventsMixin-Vue" class="headerlink" title="eventsMixin(Vue)"></a>eventsMixin(Vue)</h5><p>同样是给 <code>Vue</code> 原型挂载事件相关的方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">eventsMixin</span>(<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">    Vue.prototype.$on = <span class="function"><span class="keyword">function</span>(<span class="params">event, fn</span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">    Vue.prototype.$once = <span class="function"><span class="keyword">function</span>(<span class="params">event, fn</span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">    Vue.prototype.$off = <span class="function"><span class="keyword">function</span>(<span class="params">event, fn</span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">    Vue.prototype.$emit = <span class="function"><span class="keyword">function</span>(<span class="params">event, fn</span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="lifecycleMixin-Vue"><a href="#lifecycleMixin-Vue" class="headerlink" title="lifecycleMixin(Vue)"></a>lifecycleMixin(Vue)</h5><p>又是挂载方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">lifecycleMixin</span>(<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">    Vue.prototype._update = <span class="function"><span class="keyword">function</span>(<span class="params">vnode, hydrating</span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">    Vue.prototype.$forceUpdate = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">    Vue.prototype.$destroy = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="renderMixin-Vue"><a href="#renderMixin-Vue" class="headerlink" title="renderMixin(Vue)"></a>renderMixin(Vue)</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">renderMixin</span>(<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">    installRenderHelpers(Vue.prototype) <span class="comment">// 给Vue原型挂载了一堆快捷函数</span></span><br><span class="line">    <span class="comment">// 具体实现在 nextTick中</span></span><br><span class="line">    Vue.prototype.$nextTick = <span class="function"><span class="keyword">function</span> (<span class="params">fn</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">return</span> nextTick(fn, <span class="keyword">this</span>)</span><br><span class="line">    &#125;;</span><br><span class="line">    Vue.prototype._render = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="initGlobalAPI-Vue"><a href="#initGlobalAPI-Vue" class="headerlink" title="initGlobalAPI(Vue)"></a>initGlobalAPI(Vue)</h5><p>全局 <code>api</code>，直接挂载到 <code>Vue</code> 构造函数上</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">initGlobalAPI</span>(<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">    Vue.set = set;</span><br><span class="line">    Vue.delete = del;</span><br><span class="line">    Vue.nextTick = nextTick;</span><br><span class="line">    <span class="comment">// ...code...</span></span><br><span class="line">    initUse(Vue);</span><br><span class="line">    initMixin$<span class="number">1</span>(Vue);</span><br><span class="line">    initExtend(Vue);</span><br><span class="line">    initAssetRegisters(Vue);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以看一眼最后四个函数调用都干了什么</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">initUse</span>(<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">    Vue.use = <span class="function"><span class="keyword">function</span>(<span class="params">plugin</span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">initMixin$1</span>(<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">    Vue.mixin = <span class="function"><span class="keyword">function</span> (<span class="params">mixin</span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">initExtend</span>(<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">    Vue.extend = <span class="function"><span class="keyword">function</span> (<span class="params">extendOptions</span>) </span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// ASSET_TYPES = ["component", "directive", "filter"]</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">initAssetRegisters</span> (<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">     ASSET_TYPES.forEach(<span class="function"><span class="keyword">function</span> (<span class="params">type</span>) </span>&#123;</span><br><span class="line">         Vue[type] = <span class="function"><span class="keyword">function</span>(<span class="params">id, definition</span>)</span>&#123; <span class="comment">/** code */</span> &#125;</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>其实都只是给 <code>Vue</code> 构造函数挂载方法而已</p>
<p>再往下就是不断的初始化了，然后就是实例化 <code>vue</code> ，到这里暂时先不继续往下，先解决前面留下的比较重要的代码</p>
<h4 id="比较重要的方法分析"><a href="#比较重要的方法分析" class="headerlink" title="比较重要的方法分析"></a>比较重要的方法分析</h4><h5 id="set-方法分析"><a href="#set-方法分析" class="headerlink" title="$set 方法分析"></a>$set 方法分析</h5><h6 id="缺陷"><a href="#缺陷" class="headerlink" title="缺陷"></a>缺陷</h6><p>我们在使用 <code>vue</code> 时，假如有一个对象或数组，如</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">    name: <span class="string">'lafen'</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 或</span></span><br><span class="line"><span class="keyword">let</span> arr = [<span class="string">'123'</span>]</span><br></pre></td></tr></table></figure>
<p>当我们想给对象或数组添加新的属性的时候，直接往里面添加是无效的，即</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">obj.age = <span class="number">24</span> <span class="comment">// 这样直接添加是无法监听到数据改变的</span></span><br><span class="line">arr[<span class="number">1</span>] = <span class="string">'456'</span></span><br></pre></td></tr></table></figure>
<p>这是由于 <code>Vue</code> 所使用的是 <code>Object.defineProperty</code> 监听所导致的<strong>缺陷</strong>（此处详细会在讲<strong>响应式原理</strong>的时候细讲）</p>
<ul>
<li><code>vue</code> 无法知道对象是否添加了新的属性</li>
<li>采用数组下标去设置数组元素也同样无法监听得到（不经过<strong>拦截方法</strong>）</li>
</ul>
<h6 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h6><p>故 <code>vue</code> 提供了两个 <code>api</code> 来解决这样的缺陷，只需使用 <code>this.$set</code> 或者 <code>Vue.set</code> 来设置新属性即可将新添加的属性转换为响应式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span>.$set(obj, <span class="string">'age'</span>, <span class="number">24</span>)</span><br><span class="line"><span class="keyword">this</span>.$set(arr, <span class="number">1</span>, <span class="string">'456'</span>)</span><br></pre></td></tr></table></figure>
<p>那这两个方法是在哪里设置的呢？</p>
<h6 id="方法实现"><a href="#方法实现" class="headerlink" title="方法实现"></a>方法实现</h6><p>由前面可知，<code>$set</code> 在 <code>stateMixin</code> 中挂载到 <code>Vue</code> <strong>原型</strong>，同时在初始化全局 <code>API</code> 的时候，也挂载到了 <code>Vue</code> <strong>构造函数</strong>上，这就是我们即可以使用 <code>this.$set()</code>，又可以使用 <code>Vue.set()</code> 的原因，原型上和构造函数上都分别挂载了 <code>set</code> 函数，那么 <code>set</code> 具体实现如何？具体看看代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">set</span> (<span class="params">target, key, val</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 对未定义和简单数据类型(boolean||string||number||symbol)进行过滤</span></span><br><span class="line">    <span class="keyword">if</span> (isUndef(target) || isPrimitive(target)</span><br><span class="line">    ) &#123;</span><br><span class="line">      warn((<span class="string">"Cannot set reactive property on undefined, null, or primitive value: "</span> + ((target))));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 对数组的处理，如果是超出数组长度范围，则增加数组长度，然后通过 splice 方法替换</span></span><br><span class="line">    <span class="comment">// splice 方法是数组的拦截方法，所以会监听到修改</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">Array</span>.isArray(target) &amp;&amp; isValidArrayIndex(key)) &#123;</span><br><span class="line">      target.length = <span class="built_in">Math</span>.max(target.length, key);</span><br><span class="line">      target.splice(key, <span class="number">1</span>, val);</span><br><span class="line">      <span class="keyword">return</span> val</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 如果已经是该对象的属性了，说明已经监听了该属性，则直接赋值即可</span></span><br><span class="line">    <span class="keyword">if</span> (key <span class="keyword">in</span> target &amp;&amp; !(key <span class="keyword">in</span> <span class="built_in">Object</span>.prototype)) &#123;</span><br><span class="line">      target[key] = val;</span><br><span class="line">      <span class="keyword">return</span> val</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 不能是根组件</span></span><br><span class="line">    <span class="keyword">var</span> ob = (target).__ob__;</span><br><span class="line">    <span class="keyword">if</span> (target._isVue || (ob &amp;&amp; ob.vmCount)) &#123;</span><br><span class="line">      warn(</span><br><span class="line">        <span class="string">'Avoid adding reactive properties to a Vue instance or its root $data '</span> +</span><br><span class="line">        <span class="string">'at runtime - declare it upfront in the data option.'</span></span><br><span class="line">      );</span><br><span class="line">      <span class="keyword">return</span> val</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// target不是响应式数据，直接修改后返回</span></span><br><span class="line">    <span class="keyword">if</span> (!ob) &#123;</span><br><span class="line">      target[key] = val;</span><br><span class="line">      <span class="keyword">return</span> val</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 设置该属性为响应式，并手动触发通知</span></span><br><span class="line">    defineReactive$$<span class="number">1</span>(ob.value, key, val);</span><br><span class="line">    ob.dep.notify();</span><br><span class="line">    <span class="keyword">return</span> val</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如上代码所示</p>
<ul>
<li><code>set</code> 中对未定义的和<strong>简单数据类型</strong>的 <code>target</code> 进行过滤，包括 <code>boolean</code>、<code>string</code>、<code>number</code> 和 <code>symbol</code></li>
<li>如果 <code>target</code> 是数组，先看看 <code>key</code> 是否在数组长度范围内，是否需要延长数组索引，然后使用 <code>splice</code> 方法来进行替换新的值，由于此 <code>splice</code> 方法是 <code>Vue</code> 中定义的数组的<strong>拦截方法</strong>，故会数组的改变会是响应式更新（数组的拦截方法也会在原理一篇中讲）</li>
<li>如果 <code>key</code> 已经是 <code>target</code> 属性，说明已经是响应式了，直接赋值即可</li>
<li>如果连 <code>target</code> 都不是响应式数据，则直接赋值之后直接返回。否则使用 <code>defineReactive$$1</code> 来将其定义成响应式属性，并手动调用更新通知</li>
</ul>
<h5 id="delete方法分析"><a href="#delete方法分析" class="headerlink" title="$delete方法分析"></a>$delete方法分析</h5><h6 id="缺陷-1"><a href="#缺陷-1" class="headerlink" title="缺陷"></a>缺陷</h6><p>与 <code>$set</code> 方法类似，<code>$delete</code> 方法同样是为了解决 <code>Vue</code> 的缺陷——无法监听到对象的删除，而定义了两个方法，<code>this.$delete</code>与<code>Vue.delete</code>，具体实现看 <code>del</code> 函数中</p>
<h6 id="方法实现-1"><a href="#方法实现-1" class="headerlink" title="方法实现"></a>方法实现</h6><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">del</span> (<span class="params">target, key</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 过滤未定义的和简单数据类型的target</span></span><br><span class="line">    <span class="keyword">if</span> (isUndef(target) || isPrimitive(target)</span><br><span class="line">    ) &#123;</span><br><span class="line">      warn((<span class="string">"Cannot delete reactive property on undefined, null, or primitive value: "</span> + ((target))));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 数组，且key索引在数组范围内，直接使用拦截方法 splice</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">Array</span>.isArray(target) &amp;&amp; isValidArrayIndex(key)) &#123;</span><br><span class="line">      target.splice(key, <span class="number">1</span>);</span><br><span class="line">      <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 对象是否是 vue 实例，无法删除 vue 实例上的属性</span></span><br><span class="line">    <span class="keyword">var</span> ob = (target).__ob__;</span><br><span class="line">    <span class="keyword">if</span> (target._isVue || (ob &amp;&amp; ob.vmCount)) &#123;</span><br><span class="line">      warn(</span><br><span class="line">        <span class="string">'Avoid deleting properties on a Vue instance or its root $data '</span> +</span><br><span class="line">        <span class="string">'- just set it to null.'</span></span><br><span class="line">      );</span><br><span class="line">      <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 删除一个不存在的属性，直接返回</span></span><br><span class="line">    <span class="keyword">if</span> (!hasOwn(target, key)) &#123;</span><br><span class="line">      <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 直接 delete，然后手动更新通知</span></span><br><span class="line">    <span class="keyword">delete</span> target[key];</span><br><span class="line">    <span class="comment">// 如果target不是响应式数据，则直接返回</span></span><br><span class="line">    <span class="keyword">if</span> (!ob) &#123;</span><br><span class="line">      <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    ob.dep.notify();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可见 <code>del</code> 方法与 <code>set</code> 方法大同小异，只不过一个是赋值，一个是删除</p>
<ul>
<li>同样过滤未定义和简单数据类型</li>
<li>如果 <code>target</code> 是数组，且 <code>key</code> 在数组的索引范围内，则使用<strong>拦截方法</strong> <code>splice</code> 直接删除</li>
<li>如果不是 <code>target</code> 的属性，则直接返回</li>
<li>否则直接删除该属性，如果 <code>target</code> 不是响应式的数据，则直接返回，否则手动调用通知更新</li>
</ul>
<h5 id="on-方法分析"><a href="#on-方法分析" class="headerlink" title="$on 方法分析"></a>$on 方法分析</h5><p>事件监听函数实现，具体代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">Vue.prototype.$on = <span class="function"><span class="keyword">function</span> (<span class="params">event, fn</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">var</span> vm = <span class="keyword">this</span>;</span><br><span class="line">      <span class="comment">// 传入的自定义事件是数组，则循环调用 on 进行注册</span></span><br><span class="line">      <span class="keyword">if</span> (<span class="built_in">Array</span>.isArray(event)) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, l = event.length; i &lt; l; i++) &#123;</span><br><span class="line">          vm.$on(event[i], fn);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 将回调函数 push 进 _events[event] 属性中，不存在该属性则初始化为空数组之后再 push</span></span><br><span class="line">        (vm._events[event] || (vm._events[event] = [])).push(fn);</span><br><span class="line">        <span class="comment">// optimize hook:event cost by using a boolean flag marked at registration</span></span><br><span class="line">        <span class="comment">// instead of a hash lookup</span></span><br><span class="line">        <span class="keyword">if</span> (hookRE.test(event)) &#123;</span><br><span class="line">          vm._hasHookEvent = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> vm</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>看起来代码也很简单，只需将 <code>fn</code> 保存到对应的 <code>vm._events[event]</code> 中即可，其中 <code>fn</code> 可以是一个数组，此时遍历添加进去即可，函数触发的时候，只需根据 <code>event</code> 从 <code>vm._events</code> 中获取回调函数的数组遍历逐一执行即可</p>
<h5 id="once-方法分析"><a href="#once-方法分析" class="headerlink" title="$once 方法分析"></a>$once 方法分析</h5><p>事件监听一次性函数实现，具体代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Vue.prototype.$once = <span class="function"><span class="keyword">function</span> (<span class="params">event, fn</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">var</span> vm = <span class="keyword">this</span>;</span><br><span class="line">      <span class="comment">// 内部声明 on 函数，先使用 $off 卸载掉事件监听，在手动调用回调函数</span></span><br><span class="line">      <span class="function"><span class="keyword">function</span> <span class="title">on</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        vm.$off(event, on);</span><br><span class="line">        fn.apply(vm, <span class="built_in">arguments</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      on.fn = fn;</span><br><span class="line">      vm.$on(event, on);</span><br><span class="line">      <span class="keyword">return</span> vm</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><code>$once</code> 内部实现也很简单，其在方法内声明了一个 <code>on</code> 函数作为回调函数，然后调用 <code>$on</code> 进行事件监听。</p>
<p><code>on</code> 函数中<strong>首先</strong>使用 <code>$off</code> 将 <code>on</code> 函数解绑，<strong>然后</strong>使用 <code>apply</code> 手动调用传入的 <code>fn</code>，即实现了只执行一次事件函数则解绑的目的</p>
<h5 id="off-方法分析"><a href="#off-方法分析" class="headerlink" title="$off 方法分析"></a>$off 方法分析</h5><p><code>$off</code> 方法则比较复杂，因为可以通过传入 <code>event</code> 数组来解绑多个事件，可以传入 <code>fn</code> 解绑指定回调，也可以什么都不传，解绑所有事件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">Vue.prototype.$off = <span class="function"><span class="keyword">function</span> (<span class="params">event, fn</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">var</span> vm = <span class="keyword">this</span>;</span><br><span class="line">      <span class="comment">// all</span></span><br><span class="line">      <span class="comment">// 没有传入参数，即解除所有的事件监听，直接给 _events 对象赋值空对象即可</span></span><br><span class="line">      <span class="keyword">if</span> (!<span class="built_in">arguments</span>.length) &#123;</span><br><span class="line">        vm._events = <span class="built_in">Object</span>.create(<span class="literal">null</span>);</span><br><span class="line">        <span class="keyword">return</span> vm</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// array of events</span></span><br><span class="line">      <span class="comment">// 传入的是数组，循环解除监听</span></span><br><span class="line">      <span class="keyword">if</span> (<span class="built_in">Array</span>.isArray(event)) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i$<span class="number">1</span> = <span class="number">0</span>, l = event.length; i$<span class="number">1</span> &lt; l; i$<span class="number">1</span>++) &#123;</span><br><span class="line">          vm.$off(event[i$<span class="number">1</span>], fn);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> vm</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// specific event</span></span><br><span class="line">      <span class="comment">// 获取到 事件名 对应的所有回调函数</span></span><br><span class="line">      <span class="keyword">var</span> cbs = vm._events[event];</span><br><span class="line">      <span class="keyword">if</span> (!cbs) &#123;</span><br><span class="line">        <span class="keyword">return</span> vm</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 没传入 fn 则直接将该事件名对应的所有回调函数清空</span></span><br><span class="line">      <span class="keyword">if</span> (!fn) &#123;</span><br><span class="line">        vm._events[event] = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">return</span> vm</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// specific handler</span></span><br><span class="line">      <span class="comment">// 传入了 fn 的情况，只解除对应的回调</span></span><br><span class="line">      <span class="keyword">var</span> cb;</span><br><span class="line">      <span class="keyword">var</span> i = cbs.length;</span><br><span class="line">      <span class="keyword">while</span> (i--) &#123;</span><br><span class="line">        cb = cbs[i];</span><br><span class="line">        <span class="keyword">if</span> (cb === fn || cb.fn === fn) &#123;</span><br><span class="line">          cbs.splice(i, <span class="number">1</span>);</span><br><span class="line">          <span class="keyword">break</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> vm</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>由以上代码可知</p>
<ul>
<li>如果没有传入任何参数，则直接将 <code>vm._events</code> 置为空对象即可，即<strong>解绑所有</strong>事件</li>
<li>如果传入 <code>event</code> 是数组，则遍历调用 <code>$off</code> 来<strong>解绑多个</strong>事件</li>
<li>如果传入的 <code>event</code> 没有对应的事件回调，则直接<strong>返回</strong></li>
<li>如果没有传入 <code>fn</code> ，则将该 <code>event</code> 对应的回调数组置空，即解绑 <code>event</code> <strong>对应</strong>的所有事件回调</li>
<li>否则只解绑 <code>event</code> <strong>对应</strong>的回调函数 <code>fn</code></li>
</ul>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2019-11-30T09:04:39.251Z" itemprop="dateUpdated">2019-11-30 17:04:39</time>
</span><br>


        
        关于学习类文章网上已经有非常多，而此处文章都是我在学习过程中整理摘录的笔记，参考文章可以点击文末的参考链接进行查阅
        
    </div>
    
    <footer>
        <a href="https://lafen.gitee.io">
            <img src="/img/avatar.jpg" alt="lafen">
            lafen
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/learning/">learning</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vue/">vue</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/&title=《vue-源码笔记-初始化方法分析》 — lafen&pic=https://lafen.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/&title=《vue-源码笔记-初始化方法分析》 — lafen&source=lafen's personal blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《vue-源码笔记-初始化方法分析》 — lafen&url=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/&via=https://lafen.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">vue-源码笔记-初始化方法(2)</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">js-深入-promise模拟实现</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        打笔巨款？~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check" data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style="display:none">
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style="display:none">
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>lafen &copy; 2019 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/&title=《vue-源码笔记-初始化方法分析》 — lafen&pic=https://lafen.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/&title=《vue-源码笔记-初始化方法分析》 — lafen&source=lafen's personal blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《vue-源码笔记-初始化方法分析》 — lafen&url=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/&via=https://lafen.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lafen.gitee.io/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"positon":"right","width":150,"height":300},"mobile":{"show":true,"scale":0.8},"react":{"opacity":0.8,"opacityDefault":0.8,"opacityOnHover":0.6},"log":false});</script></body>
</html>
